package com.ebelter.seneo.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.beans.httpresponse.GetAverageResponse;
import com.ebelter.seneo.ui.views.LineView;
import com.ebelter.seneo.utils.SeneoUtils;
import com.raizlabs.android.dbflow.list.IFlowCursorIterator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;




public class TrendHLvAdapter extends RecyclerView.Adapter<TrendHLvAdapter.ViewHolder> {



    private Context mContext;

    private List<GetAverageResponse.ResultDataBean.DataBean> dayDatas;
    private List<GetAverageResponse.ResultDataBean.DataBean> weekDatas;
    private List<GetAverageResponse.ResultDataBean.DataBean>  monthDatas;



    public void clearData() {
        if (dayDatas!=null){
            dayDatas.clear();
        }
        if (weekDatas!=null){
            weekDatas.clear();
        }
        if (monthDatas!=null){
            monthDatas.clear();
        }
        notifyDataSetChanged();
    }


    public static final int avgWeight = 1;
    public static final int avgAR = 2;
    public static final int avgMQ = 3;

    public static final int DAY= 4;
    public static final int WEEK = 5;
    public static final int MONTH = 6;


    //1avgWeight 2avgAR 3avgMQ 0avgHR
    private int currentDisplayType=avgWeight;
    //4DAY 5WEEK 6MONTH
    private int currentDisplayTimeType=DAY;



    public void setCurrentDisplayTimeType(int currentDisplayTimeType) {
        this.currentDisplayTimeType = currentDisplayTimeType;
    }

    //设置当前显示的类型
    public void setCurrentDisplayType(int currentDisplayType) {
        this.currentDisplayType = currentDisplayType;
        calculator();
        notifyDataSetChanged();
    }

//    public void setCurrentDisplayType2(int currentDisplayType) {
//        this.currentDisplayType = currentDisplayType;
//    }


    public int getCurrentDisplayType() {
        return currentDisplayType;
    }

    public TrendHLvAdapter(Context context){
        this.mContext=context;
        initList();
        calculator();
    }



    private void initList(){
        if (dayDatas==null){
            dayDatas=new ArrayList<>();
        }
        if (weekDatas==null){
            weekDatas=new ArrayList<>();
        }
        if (monthDatas==null){
            monthDatas=new ArrayList<>();
        }
    }

    @NonNull
    @Override
    public TrendHLvAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.trendhlv_item_ly,null);
        return new ViewHolder(view);
    }




    private int unit;//0kg 1lb

    public void setUnit(int unit) {
        this.unit = unit;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull TrendHLvAdapter.ViewHolder holder, int position) {

        float linePer = 0;
        String time = null;
        String toptitle = null;
        List<GetAverageResponse.ResultDataBean.DataBean> dataTemp = getDisplayDatas();
        if (dataTemp==null){
            linePer=0;
            time="";
        }else {
            if (currentDisplayType==avgWeight){
                linePer=maxWeight==0?0:(dataTemp.get(position).avgWeight/maxWeight);
                float weightF = (unit==0?dataTemp.get(position).avgWeight:SeneoUtils.kg2lb(dataTemp.get(position).avgWeight));
                toptitle = NumUtils.numBaoLiuWei1(weightF)+"";
            }else if (currentDisplayType==avgAR){
                linePer=maxAR==0?0:(dataTemp.get(position).avgAR/maxAR);
                toptitle = NumUtils.numBaoLiuWei1(dataTemp.get(position).avgAR)+"";
            }else if (currentDisplayType==avgMQ){
                linePer=maxMQ==0?0:(dataTemp.get(position).avgMQ/maxMQ);
                float musF = (unit==0?dataTemp.get(position).avgMQ:SeneoUtils.kg2lb(dataTemp.get(position).avgMQ));
                toptitle = NumUtils.numBaoLiuWei1(musF)+"";
            }
            if (currentDisplayTimeType==DAY){
                time= SeneoUtils.timeFatmat(dataTemp.get(position).days,1);
            }else if (currentDisplayTimeType==WEEK){
                time=SeneoUtils.intNumFatmat7(dataTemp.get(position).days);
            }else if(currentDisplayTimeType==MONTH){
                time=SeneoUtils.monthFatmat(dataTemp.get(position).days);
            }
        }


        holder.lineView.setValues(linePer,time,toptitle,1.0f);
    }

    @Override
    public int getItemCount() {
        if (currentDisplayTimeType==DAY){
            return dayDatas==null?0:dayDatas.size();
        }else if (currentDisplayTimeType==WEEK){
            return weekDatas==null?0:weekDatas.size();
        }else if(currentDisplayTimeType==MONTH){
            return monthDatas==null?0:monthDatas.size();
        }
        return 0;
    }

    private List<GetAverageResponse.ResultDataBean.DataBean> getDisplayDatas(){
        if (currentDisplayTimeType==DAY){
           return dayDatas;
        }else if (currentDisplayTimeType==WEEK){
            return weekDatas;
        }else if(currentDisplayTimeType==MONTH){
            return monthDatas;
        }
        return null;
    }


    //whichTime 1DAY 2WEEK 3MONTH
    public void addDatas(List<GetAverageResponse.ResultDataBean.DataBean> data, int whichTime) {
        if (whichTime==1){
            //dayDatas.addAll(0,data);
            addDatas2(dayDatas,data);
        }else if (whichTime==2){
            //weekDatas.addAll(0,data);
            addDatas2(weekDatas,data);
        }else if(whichTime==3){
          //  monthDatas.addAll(0,data);
            addDatas2(monthDatas,data);
        }
        calculator();
        notifyDataSetChanged();
    }



    private void addDatas2(List<GetAverageResponse.ResultDataBean.DataBean> bigList,List<GetAverageResponse.ResultDataBean.DataBean> subList){

        if (bigList==null||subList==null||subList.size()==0){
            return;
        }
        List<GetAverageResponse.ResultDataBean.DataBean> resutltList = new ArrayList();
        for (GetAverageResponse.ResultDataBean.DataBean subBean:subList){
            boolean isContain = isContain(bigList,subBean);
            if (!isContain){
                resutltList.add(subBean);
            }
        }
        bigList.addAll(0,resutltList);

    }


    private boolean isContain(List<GetAverageResponse.ResultDataBean.DataBean> bigList,
                              GetAverageResponse.ResultDataBean.DataBean subBean){

        for(GetAverageResponse.ResultDataBean.DataBean bigBean:bigList){
            if (TextUtils.equals(bigBean.days,subBean.days)){
                return true;
            }
        }
        return false;
    }


    //TYPE_WEIGHT 1
    //TYPE_BODYFAT 2
    //TYPE_MUSCLE 3
    //TIME_DAY 4
    //TIME_WEEK 5
    //TIME_MONTH 6
    public float getAvg(int TIME_TYPE,int GN_TYPE){
        List<GetAverageResponse.ResultDataBean.DataBean> data = getDataByTimeType(TIME_TYPE);
        if (data==null||data.size()==0){
            return 0;
        }
        if (avgWeight==GN_TYPE){
            return data.get(data.size()-1).avgWeight;
        }else if (avgAR==GN_TYPE){
            return data.get(data.size()-1).avgAR;
        }else if (avgMQ==GN_TYPE){
            return data.get(data.size()-1).avgMQ;
        }
        return 0;
    }

    //TYPE_WEIGHT 1
    //TYPE_BODYFAT 2
    //TYPE_MUSCLE 3
    //TIME_DAY 4
    //TIME_WEEK 5
    //TIME_MONTH 6
    public float getChange(int TIME_TYPE,int GN_TYPE){
        List<GetAverageResponse.ResultDataBean.DataBean> data=getDataByTimeType(TIME_TYPE);
        if (data==null||data.size()==0||data.size()==1){
            return 0;
        }else if (data.size()>1){
            int unit = SeneoUtils.getUnitModel();
            if (avgWeight==GN_TYPE){
                float f1= NumUtils.numBaoLiuWei1(SeneoUtils.adapterDataFloat(data.get(data.size()-1).avgWeight,unit));//最后一条
                float f2=NumUtils.numBaoLiuWei1(SeneoUtils.adapterDataFloat(data.get(data.size()-2).avgWeight,unit));//倒数第二条
                return f1-f2;
            }else if (avgAR==GN_TYPE){
                float f3= NumUtils.numBaoLiuWei1(data.get(data.size()-1).avgAR);
                float f4=NumUtils.numBaoLiuWei1(data.get(data.size()-2).avgAR);
                return f3-f4;
            }else if (avgMQ==GN_TYPE){
                float f5= NumUtils.numBaoLiuWei1(SeneoUtils.adapterDataFloat(data.get(data.size()-1).avgMQ,unit));
                float f6=NumUtils.numBaoLiuWei1(SeneoUtils.adapterDataFloat(data.get(data.size()-2).avgMQ,unit));
                return f5-f6;
            }
        }

        return 0;
    }

    public String getChangeStr(int TIME_TYPE,int GN_TYPE){

        float change = getChange(TIME_TYPE,GN_TYPE);
        if (change==0){
            return NumUtils.numBaoLiuWei1(change)+"";
        }else if (change>0){
            return "+"+Math.abs(NumUtils.numBaoLiuWei1(change));
        }else if (change<0){
            return "-"+Math.abs(NumUtils.numBaoLiuWei1(change));
        }
        return null;
    }

    private List<GetAverageResponse.ResultDataBean.DataBean> getDataByTimeType(int TIME_TYPE){
        List<GetAverageResponse.ResultDataBean.DataBean> data=null;
        if (TIME_TYPE==DAY){
            data = dayDatas;
        }else if (TIME_TYPE==WEEK){
            data = weekDatas;
        }else if (TIME_TYPE==MONTH){
            data = monthDatas;
        }
        return data;
    }



    //ViewHolder，需要继承RecyclerView.ViewHolder
    static class ViewHolder extends RecyclerView.ViewHolder{
        private LineView lineView;
        ViewHolder(View itemView) {
            super(itemView);
            lineView = itemView.findViewById(R.id.trendhlv_item_lineview);
        }
    }

    public float maxWeight;
    public float maxAR;
    public float maxMQ;
    public float maxHR;
    //计算最大值
    public void calculator(){
        List<GetAverageResponse.ResultDataBean.DataBean> datas = getDisplayDatas();;


        if (datas==null||datas.size()==0){
            maxWeight=0;
            maxAR=0;
            maxMQ=0;
            maxHR=0;

        }else if (datas.size()==1){
            maxWeight= datas.get(0).avgWeight;
            maxAR=  datas.get(0).avgAR;
            maxMQ= datas.get(0).avgMQ;
            maxHR= datas.get(0).avgHR;
        }else {
            GetAverageResponse.ResultDataBean.DataBean maxWeight1 = Collections.max(datas, new Comparator<GetAverageResponse.ResultDataBean.DataBean>() {
                @Override
                public int compare(GetAverageResponse.ResultDataBean.DataBean o1, GetAverageResponse.ResultDataBean.DataBean o2) {
                    if (o1.avgWeight<o2.avgWeight){
                        return -1;
                    }else if (o1.avgWeight>o2.avgWeight){
                        return 1;
                    }else {
                        return 0;
                    }

                }
            });
            GetAverageResponse.ResultDataBean.DataBean maxAR1= Collections.max(datas, new Comparator<GetAverageResponse.ResultDataBean.DataBean>() {
                @Override
                public int compare(GetAverageResponse.ResultDataBean.DataBean o1, GetAverageResponse.ResultDataBean.DataBean o2) {
                    if (o1.avgAR<o2.avgAR){
                        return -1;
                    }else if (o1.avgAR>o2.avgAR){
                        return 1;
                    }else {
                        return 0;
                    }

                }
            });

            GetAverageResponse.ResultDataBean.DataBean maxMQ1= Collections.max(datas, new Comparator<GetAverageResponse.ResultDataBean.DataBean>() {
                @Override
                public int compare(GetAverageResponse.ResultDataBean.DataBean o1, GetAverageResponse.ResultDataBean.DataBean o2) {
                    if (o1.avgMQ<o2.avgMQ){
                        return -1;
                    }else if (o1.avgMQ>o2.avgMQ){
                        return 1;
                    }else {
                        return 0;
                    }

                }
            });



            maxWeight=  maxWeight1.avgWeight;
            maxAR= maxAR1.avgAR;
            maxMQ=  maxMQ1.avgMQ;


        }
        maxWeight= (float) (maxWeight*1.2);
        maxAR= (float) (maxAR*1.2);
        maxMQ= (float) (maxMQ*1.2);
        maxHR= (float) (maxHR*1.2);//把最大值弄大0.2倍

    }


}
